import java.io.BufferedWriter;
import java.io.File;
import java.io.FileOutputStream;
import java.io.FileWriter;
import java.sql.*;
import config.ConfigManager;

import datamanagers.*;


public class TableScores {
	public static void main(String[] args) throws DataManagerException{
		init();
	}
	public static void init() throws DataManagerException{
		
		PreparedStatement statement = null;
		Connection connection = null;
		
		try {
		  //    FileOutputStream file = new FileOutputStream("scores.dat");

			File current_dir = new File("");
			String scorespath = current_dir.getAbsolutePath().toString()
			+ System.getProperty("file.separator") + "scores.dat";
			if (System.getProperty("file.separator").equals("\\"))
				scorespath = scorespath.replace("\\", "\\\\");
			
			ConfigManager config = ConfigManager.getInstance();
			DatasourceMySql datasource = new DatasourceMySql();
			connection = datasource.getConnection();
						
			String sql = String.format("SELECT ProfileId,UserId,Rating FROM %s.tbl_ratings ORDER BY ProfileID,UserId INTO OUTFILE '%s' FIELDS TERMINATED BY ',' LINES TERMINATED BY '\n'",
					config.get("db.schema"),scorespath); 
			
			System.out.print(sql);
			/*
			statement = connection.prepareStatement(sql);
			System.out.println("Scrittura su file in corso...");
			statement.execute();
			System.out.println("fatto!");
			
			sql = String.format("LOAD DATA LOCAL INFILE '%s' INTO TABLE %s.tbl_scores FIELDS TERMINATED BY ',' LINES TERMINATED BY '\n' (@col1,@col2,@col3) SET ProfileID=@col1,UserID=@col2,Score=@col3;",
					scorespath, config.get("db.schema"));
			
			
			
			System.out.println("Travaso sul db in corso...");
			statement = connection.prepareStatement(sql);
			statement.execute();
			System.out.println("fatto! Bella!!! :D"); */
		} catch (Exception e) {
			throw new DataManagerException(e);
		}finally{
			try{
				connection.close();
				statement.close();
				
			}catch(Exception e){
				
			}
		}
	}
}
